package com.ruangong.GitClient.app;

import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.ruangong.GitClient.R;
import com.ruangong.GitClient.HunHePanAPI.*;


import java.util.ArrayList;
import java.util.List;

public class MovieSearch extends Fragment {

    private ListView searchResultListView;
    private ArrayAdapter<MovieLink> adapter;
    private List<MovieLink> searchResults;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_movie_search, container, false);

        searchResultListView = rootView.findViewById(R.id.searchResultListView);

        // 初始化搜索结果列表
        searchResults = new ArrayList<>();
        adapter = new MovieLinkAdapter(getContext(), searchResults);

        searchResultListView.setAdapter(adapter);

        // 设置搜索框的搜索功能
        SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = rootView.findViewById(R.id.searchView);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
        searchView.setIconifiedByDefault(false);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                // 在用户提交搜索关键字时调用
                performSearch(query);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                // 在搜索框中的文字发生变化时调用
                // 如果需要实时搜索功能，可以在这里处理
                return false;
            }
        });

        // 设置列表项的点击事件
        searchResultListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 获取点击的项的内容
                MovieLink clickedMovieLink = searchResults.get(position);
                // 创建跳转到另一个Activity的意图
                Intent intent = new Intent(getActivity(), ResourceDetails.class);
                // 在Intent中传递额外的数据，如果需要的话
                intent.putExtra("MOVIE_LINK", clickedMovieLink);
                startActivity(intent);
            }
        });

        return rootView;
    }

    // 执行搜索操作
    private void performSearch(String query) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 在这里执行网络请求
                List<MovieLink> movieLinks = Test2.getResult(query);
                Log.d("SearchActivity", "查询结果: " + movieLinks);
                searchResults.clear();
                if (movieLinks == null || movieLinks.isEmpty()) {
                    searchResults.add(new MovieLink("", "未找到查询结果", "", "")); // 添加一个标记项
                    searchResultListView.setOnItemClickListener(null);
                } else {
                    // 将搜索结果添加到 searchResults 列表中
                    searchResults.addAll(movieLinks);
                }
                // 切换回主线程更新UI
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // 更新UI
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        }).start();
    }
}


